
# 标题：胜利龟
# 作者：陈兴权
# Data：4/24/2021

import math
import random
import game.tcturtle


class CombatTurtle(game.tcturtle.TurtleParent):
    """Wanderer combat turtle.

    Wanders around randomly until it gets close enough to the opponent, after
    which it directly pursues them.
    """

    # -------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.
        """

        return "胜利龟"

    # -------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.
        """

        return "Wanders randomly and pursues when close enough."

    # -------------------------------------------------------------------------

    def class_shape():

        return 0

    # =========================================================================

    def setup(self):
        """CombatTurtle.setup() -> None
        Initialization code for Combat Turtle.
        """

        # 定义一个随机标题和一个重新随机化的等待时间
        (self.head, self.wait) = self.rerandomize()

        # 定义追击截止距离
        self.pursuit_range = 0.75 * self.missile_range

        # 定义要扫描的海龟周围的相对极坐标
        self.nose_rel = (8, 0.0)  # just ahead of turtle's front
        self.hand_rel = (8, math.pi / 2)  # to left of turtle

    # -------------------------------------------------------------------------

    def step(self):


        # 减量重新随机化的计时器
        self.wait -= 1

        # 如果计时器用完，重新随机化标题和计时器
        if self.wait <= 0:
            (self.head, self.wait) = self.rerandomize()

       # self.turn_towards()

        #-----------------------------------
        nose = ((self.x + self.nose_rel[0] *
                 math.cos(math.radians(self.heading) + self.nose_rel[1])),
                (self.y - self.nose_rel[0] *
                 math.sin(math.radians(self.heading) + self.nose_rel[1])))
        hand = ((self.x + self.hand_rel[0] *
                 math.cos(math.radians(self.heading) + self.hand_rel[1])),
                (self.y - self.hand_rel[0] *
                 math.sin(math.radians(self.heading) + self.hand_rel[1])))

        # 根据鼻子和手是否清晰来判断行为
        if self.free_space(nose) == True:
            # 前方无障碍物时往前走
            self.turn_towards()
            # self.forward()
        else:
            if self.free_space(hand) == True:
                # 如果有时间向左，向左转

                self.left()
             #   self.turn_towards()
                self.forward()
            else:
                # 如果前面堵住了，向左拐，向右拐
                self.right()
              #  self.turn_towards()
                self.forward()








        #----------------------------
        # 根据与对手的距离来决定行为
        #self.turn_towards()
        if (self.distance() <= self.pursuit_range and
                self.line_of_sight()):
            # 在追击范围内，直接向对手移动

            # 转向对手

            # self.turn_towards()

            # 向对手移动(如果太近则移开)
            if self.distance() > 1 * self.missile_radius:
                self.turn_towards(self.head)
                self.backward()
               # self.forward()
            else:
               # self.turn_towards()
               self.turn_towards(self.head)
               self.forward()
               self.backward()
               self.backward()
            # ------------------------------------------


            # 如果对手在视线范围内，可以射击
            if (self.can_shoot and abs(self.relative_heading_towards()) <=5
                    and self.line_of_sight() and self.distance() <= self.missile_range):
                self.turn_towards()
                self.shoot()
               # self.backward()
               # self.turn_towards(self.head)

        else:
            # 在追踪范围外，随意游荡
          #  self.turn_towards()
            # 转向随机选择的方向并向前移动
            self.forward()
           # self.turn_towards(self.head)
            #self.turn_towards()
            self.forward()
            # self.turn_towards()

    # =========================================================================

    def rerandomize(self):


        rh = random.randrange(-1, 180)  # 随机的标题
        rt = random.randrange(5, 10)  # 随机时间

        return (rh, rt)

    # -------------------------------------------------------------------------
